package org.i77soft.games.LGame.engine;

import java.lang.reflect.Method;

import org.i77soft.games.MineAcerLite.Main;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class AppConfig {

    private static final int DEBUG = 0x00000003;
    private static final int SCREEN_LDPI = 1;
    private static final int SCREEN_MDPI = 2;
    private static final int SCREEN_IPONE4 = 3;
    private static final int SCREEN_HDPI = 4;
    private Activity parent = null;
    private boolean lanscape = true;
    private int screenWidth = 0;
    private int screenHeight = 0;
    private int maxScreenWidth = 0;
    private int maxScreenHeight = 0;

    private static Method realWidthMethod = null;
    private static Method realHeightMethod = null;

    public AppConfig(Activity parent) {
        this.parent = parent;
        getScreenSize(parent);
    }

    @SuppressWarnings("unused")
    public AppConfig(int screenWidth, int screenHeight) {
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
    }

    public boolean getLanscape() {
        return lanscape;
    }

    public void setLanscape(boolean lanscape) {
        this.lanscape = lanscape;
    }

    public Activity getParent() {
        return parent;
    }

    public void setParent(Activity parent) {
        this.parent = parent;
    }

    @SuppressWarnings("unused")
    public int getScreenWidth() {
        return screenWidth;
    }

    public void setScreenWidth(int screenWidth) {
        this.screenWidth = screenWidth;
    }

    @SuppressWarnings("unused")
    public int getScreenHeight() {
        return screenHeight;
    }

    public void setScreenHeight(int screenHeight) {
        this.screenHeight = screenHeight;
    }

    public int getMaxScreenWidth() {
        return maxScreenWidth;
    }

    public void setMaxScreenWidth(int maxScreenWidth) {
        this.maxScreenWidth = maxScreenWidth;
    }

    public int getMaxScreenHeight() {
        return maxScreenHeight;
    }

    public void setMaxScreenHeight(int maxScreenHeight) {
        this.maxScreenHeight = maxScreenHeight;
    }

    /**
     * getScreenSize0
     */
    public void getScreenSize0(Activity window) {
        Display display = null;
        if (window != null) {
            // 获取屏幕高宽
            display = window.getWindowManager().getDefaultDisplay();
            if (display != null) {
                setScreenWidth(display.getWidth());
                setScreenHeight(display.getHeight() - 64);
            }
        }
    }

    /**
     * getScreenSize
     */
    public void getScreenSize(Activity window) {
        Display display = null;
        int statusBarHeight = 0;
        int realScreenWidth = 0, realScreenHeight = 0;
        Boolean bIsUnknown = true;
        try {
            if (window != null) {
                String sdk_version_str = android.os.Build.VERSION.SDK;
                @SuppressWarnings("unused")
                String release_version_str = android.os.Build.VERSION.RELEASE;
                int sdk_version = Integer.parseInt(sdk_version_str);
                if (sdk_version >= android_os_Build.VERSION_CODES.DONUT
                        && sdk_version <= android_os_Build.VERSION_CODES.GINGERBREAD_MR1
                // && (sdk_version != android_os_Build.VERSION_CODES.GINGERBREAD_MR1)
                ) {
                    // android 1.6-2.x ==> [DONUT:4, GINGERBREAD_MR1:10]
                    /*
                    Rect rect = new Rect();
                    window.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                    //*/
                    // 状态栏高度, 默认值为 25
                    statusBarHeight = 0;
                    // statusBarHeight = rect.top;
                    // 获取屏幕高宽
                    WindowManager wm = (WindowManager) window.getSystemService(Context.WINDOW_SERVICE);
                    if (wm != null) {
                        display = wm.getDefaultDisplay();
                        if (display != null) {
                            realScreenWidth = display.getWidth();
                            realScreenHeight = display.getHeight();
                        }
                    }
                    bIsUnknown = false;
                } else if ( // (sdk_version == android_os_Build.VERSION_CODES.GINGERBREAD_MR1) ||
                (sdk_version >= android_os_Build.VERSION_CODES.HONEYCOMB
                && sdk_version <= android_os_Build.VERSION_CODES.HONEYCOMB_MR1)) {
                    // android 3.0-3.1 ==> [HONEYCOMB:11, HONEYCOMB_MR2:12]
                    /*
                    Rect rect = new Rect();
                    window.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                    //*/
                    // 获取屏幕高宽
                    display = window.getWindowManager().getDefaultDisplay();
                    if (display != null) {
                        realScreenWidth = display.getWidth();
                        realScreenHeight = display.getHeight();
                    }
                    statusBarHeight = 48;
                    // statusBarHeight = realScreenHeight - rect.bottom;
                    statusBarHeight = 0;
                    bIsUnknown = false;
                } else if (sdk_version >= android_os_Build.VERSION_CODES.HONEYCOMB_MR2
                        && sdk_version <= android_os_Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR2) {
                    // android 3.2-4.x ==> [HONEYCOMB_MR2:13, ICE_CREAM_SANDWICH_MR2:16]
                    /*
                    Rect rect = new Rect();
                    window.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                    //*/
                    WindowManager wm = (WindowManager) window.getSystemService(Context.WINDOW_SERVICE);
                    display = wm.getDefaultDisplay();
                    try {
                        if (realWidthMethod == null) {
                            // getRealWidth()是隐函数, 所以利用反射调用
                            realWidthMethod = display.getClass().getMethod("getRealWidth");
                        }
                        if (realHeightMethod == null) {
                            // getRealHeight()是隐函数, 所以利用反射调用
                            realHeightMethod = display.getClass().getMethod("getRealHeight");
                        }
                        realScreenWidth = (Integer) realWidthMethod.invoke(display);
                        realScreenHeight = (Integer) realHeightMethod.invoke(display);
                    } catch (Exception ex) {
                        //
                    }
                    statusBarHeight = 48;
                    // statusBarHeight = realScreenHeight - rect.bottom;
                    statusBarHeight = 0;
                    bIsUnknown = false;
                } else if (sdk_version > android_os_Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR2) {
                    // android 4.x
                } else {
                    // unknown version
                }
                if (!bIsUnknown) {
                    setScreenWidth(realScreenWidth);
                    setScreenHeight(realScreenHeight - statusBarHeight);
                    setMaxScreenWidth(realScreenWidth);
                    setMaxScreenHeight(realScreenHeight);
                    if (realScreenWidth < realScreenHeight)
                        lanscape = false;
                    else
                        lanscape = true;

                    Log.println(DEBUG, "MainActivity", "realScreenWidth = "
                            + realScreenWidth);
                    Log.println(DEBUG, "MainActivity", "realScreenHeight = "
                            + realScreenHeight);
                    Log.println(DEBUG, "MainActivity", "statusBarHeight = "
                            + statusBarHeight);
                    Log.println(DEBUG, "MainActivity", "lanscape = "
                            + lanscape);
                }
            }
        } catch (Exception ex) {
            //
        }
    }

    public int getScreenHDMode() {
        int nScreenHDMode = SCREEN_LDPI;
        int nMaxScreenWidth, nMaxScreenHeight;
        if (maxScreenWidth > maxScreenHeight) {
            nMaxScreenWidth = maxScreenWidth;
            nMaxScreenHeight = maxScreenHeight;
        }
        else {
            nMaxScreenWidth = maxScreenHeight;
            nMaxScreenHeight = maxScreenWidth;
        }

        if (nMaxScreenWidth >= 1024 && nMaxScreenHeight >= 768) {
            nScreenHDMode = SCREEN_HDPI;
        }
        else if (nMaxScreenWidth >= 960 && nMaxScreenHeight >= 640) {
            nScreenHDMode = SCREEN_IPONE4;
        }
        else if (nMaxScreenWidth >= 800 && nMaxScreenHeight >= 480) {
            nScreenHDMode = SCREEN_MDPI;
        }
        else {
            nScreenHDMode = SCREEN_LDPI;
        }
        return nScreenHDMode;
    }

    public boolean isHDMode() {
        int nScreenHDMode = getScreenHDMode();
        if (nScreenHDMode >= SCREEN_HDPI)
            return true;
        else
            return false;
    }

}
